// pages/choose_list/list.js
const app = getApp();
var request = require("../../utils/request.js");
var wx_alert = require("../../utils/wx_alert.js");
var page = 1;
var limit = 5;
var sort = "1";
var brandId = "0";
var lat = "";
var lit = "";
var classifyId = "0";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected_cate:false,
    all_lists:[    
    ],
    stores:[
    ],
    first_select:0,
    storelists:[
    ],
    sorts: "false",
    show_have:true,
    first_id:"1",
    input_search:"",
    city:"南京",
    first_index:"0",
    all_classify:"全部分类",
    brand:"品牌"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    app.bindUser(options);//绑定上下级
    var that = this;
    if (options.storeId){
      classifyId = options.storeId
      this.setData({
        first_select: options.storeId,
      })
    }else{
      classifyId = 0;
      this.setData({
        all_classify:"全部分类"
      })
    }
    if (options.storelist){
      brandId = options.storelist;
      this.setData({
        second_two: options.storeId,
      })      
    }else{
      brandId = 0;
      this.setData({
        brand: "品牌"
      })
    }
    //快捷导航跳转
    this.turn = request.turn;
    this.close_modal = request.close_modal;
    this.setData({
      img_url: request.url + "/",
    })

    var data = {};
    request.request(app.globalData.classifyList, data, function (res) {
      console.log(res)
      var stores = [];
      if(res.code == 0){
        var classify_name;
        for (var i in res.classifyList){
          if (res.classifyList[i].children.length>0){
            stores.push(res.classifyList[i].children)
          }
          if (classifyId == res.classifyList[i].id){
            classify_name = res.classifyList[i].name;
          }
        }
        that.setData({
          all_lists: res.classifyList,
          stores: stores,
          all_classify: classify_name
        })
      }
    })
    
    request.request(app.globalData.brandList,{},function(res){
      console.log(res)
      if(res.code==0){
        var brand;
        console.log(brandId)
        for (var j in res.brandList){
          if (brandId == res.brandList[j].id){
            brand = res.brandList[j].name
          }
        }
        that.setData({
          storelists: res.brandList,
          brand: brand
        })
        
      }

    })


  },
  //数据
  getStoreList: function (){
    var that = this;
    var data = {
      limit: limit,
      page: page,
      lat: lat,
      lit: lit,
      city: that.data.city,
      classifyId: classifyId,
      keyword: this.data.input_search,
      brandId: brandId,
      sort: sort
    }
    request.request(app.globalData.getStoreList, data,function(res){
      console.log(res)
      if(res.code == 0){
        for (var i in res.storeExlist){

          var label =  res.storeExlist[i].label;
          res.storeExlist[i].label = label.split(",");
        }
        that.setData({
          storeExlist: res.storeExlist
        })
      } else {
        wx_alert.alert(res.msg)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //位置
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        lat = res.latitude;
        lit = res.longitude;
        wx.setStorageSync('lat', lat);
        wx.setStorageSync('lit', lit);
        if (lat && lit) {
          that.getStoreList();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user_info = wx.getStorageSync('totalInfo');
    return {
      title: "云家故事",
      path: "/pages/choose_list/list?userId=" + user_info.userId
    }
  },
  //选择列别
  choose_selected:function(e){
    var that = this;
    var types = e.currentTarget.dataset.type;
    var selected_cate = this.data.selected_cate;
    var sorts = this.data.sorts;
    page = 1
    this.setData({
      selected_cate:types,
      sorts:true
    })
     if(types=="all"){

     } else if (types == "store"){


     }else{
       
       if ( !sorts ){
         sort = 1;
       }else{
         sort = 2;
       }
       sorts = !sorts;       
       this.setData({
         sorts: sorts
       })
       that.getStoreList();
     }
  },
  //分类一级分类
  first_select:function(e){
    // console.log(e)
    var first_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    this.setData({
      first_select: first_id,
      first_id: first_id,
      first_index: index
    })
  },
  //分类二级分类
  second_third: function (e) {
    var secId = e.currentTarget.dataset.secid;
    var name = e.currentTarget.dataset.name;       
    classifyId = secId;
    this.getStoreList();
    this.setData({
      second_two: secId,
      selected_cate: "",
      all_classify:name
    })
  },
  close_model:function(){
    this.setData({
      selected_cate:""
    })
  },
  //品牌分类  
  second_select:function(e){
    var index = e.currentTarget.dataset.index;
    var name = e.currentTarget.dataset.name;    
    brandId = index;
    this.getStoreList();        
    this.setData({
      second_select:index,
      selected_cate:"",
      brand: name      
    })
  },
  // check_more:function(){
  //   wx.navigateTo({
  //     url: '/pages/store_details/store_details',
  //   })
  // },
  tap_product_detail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/phone_detail/phone_detail?productId=' + id,
    })
  },
  //上拉加载
  scroll:function(){
    page++;
    var that = this;
    var storeExlist = this.data.storeExlist;
    var data = {
      limit: limit,
      page: page,
      lat: lat,
      lit: lit,
      city: "南京",
      classifyId: classifyId,
      keyword: this.data.input_search,
      brandId: brandId,
      sort: sort
    }
    request.request(app.globalData.getStoreList, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        for (var i in res.storeExlist){
          storeExlist.push(res.storeExlist[i])
        }
        that.setData({
          storeExlist: storeExlist
        })
      }
    })
  },
  // 输入搜索
  input_search:function(e){
    console.log(e)
    var input_search = e.detail.value;
    this.setData({
      input_search: input_search,
   
    })
  },
  //
  search_sort:function(){
    brandId = 0;
    classifyId = 0;
    this.getStoreList();
    this.setData({
      all_classify: "全部分类",
      brand: "品牌"   
    })    
  }
})